import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Icon } from '/@/components/Icon';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '/@/api/demo/system';
import { useMessage } from '/@/hooks/web/useMessage';
import { useI18n } from '/@/hooks/web/useI18n';
import { defHttp } from '/@/utils/http/axios';
enum Api {
  refreshCacheUrl = '/sys/spaUserRole/refreshCache',
  menuTrueAll = '/spa/spaMenu/menuTrueAll',
  PageList = '/spa/spaMenu/menuTrue',
  Add = '/spa/spaMenu/add',
  Edit = '/spa/spaMenu/edit',
  Delete = '/spa/spaMenu/delete?id=',
}
const { t } = useI18n();
export const refreshCache = (params?: any) =>
  defHttp.get<any>({ url: Api.refreshCacheUrl, params });
//
export const getmenuTrueAll = (params?: any) => defHttp.get<any>({ url: Api.menuTrueAll, params });
//
export const getListByPage = (params?: any) => defHttp.get<any>({ url: Api.PageList, params });
//
export const addSpaMenu = (params?: any) => {
  return defHttp.post<any>({ url: Api.Add, params });
};

export const editSpaMenu = (params?: any) => {
  return defHttp.put<any>({ url: Api.Edit, params });
};

/**
 * 删除字典
 */
export const deleteSpaMenu = (id, handleSuccess) => {
  return defHttp.delete({ url: Api.Delete + id }).then(() => {
    handleSuccess();
  });
};

export const columns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'title',
    width: 200,
  },
  {
    title: '路径',
    dataIndex: 'path',
    width: 200,
  },
  {
    title: '组件',
    dataIndex: 'component',
    width: 200,
  },
  {
    title: '排序',
    dataIndex: 'orderNo',
    width: 200,
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 200,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  {
    title: '权限标识',
    dataIndex: 'permission',
    width: 200,
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'parentId',
    label: '上级菜单',
    component: 'ApiTreeSelect',
    required: false,
    componentProps: {
      api: getmenuTrueAll,
      replaceFields: {
        title: 'title',
        key: 'id',
        value: 'id',
      },
      dropdownStyle: {
        maxHeight: '50vh',
      },
    },
  },
  {
    field: 'id',
    label: '编号',
    required: false,
    component: 'Input',
    show: false,
  },
  {
    field: 'parentId',
    label: '父级编号',
    required: false,
    component: 'Input',
    show: false,
  },
  {
    field: 'path',
    label: '路由',
    required: false,
    component: 'Input',
  },
  {
    field: 'component',
    label: '组件',
    required: false,
    component: 'Input',
  },
  {
    field: 'orderNo',
    label: '排序',
    required: false,
    component: 'Input',
    defaultValue: '', //必需，去掉后第一次在输入框打汉字控制台会报error
    componentProps: ({ formModel }) => {
      return {
        oninput: (e) => {
          formModel.orderNo = formModel.orderNo.replace(/\D*/g, '');
        },
      };
    },
  },
  {
    field: 'icon',
    label: '菜单图标',
    component: 'IconPicker',
  },
  {
    field: 'title',
    label: '名称',
    required: false,
    component: 'Input',
  },
  {
    field: 'permission',
    label: '权限标识',
    required: false,
    component: 'Input',
  },
];
